package com.groupseven.happycampus.common.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;

import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @CreateTime: 2022-07-20 19:08
 * oos  文件远程远程上传下载管理
 */
public class OSSUtil {

    public static String uploadFile(InputStream inputStream, String filename){

        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tL24pdXCY4A2zvPwxv4";
        String accessKeySecret = "YsVBhSjmDI2u1EnFAmCjnbSJ1QMQY2";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "minissm2207";
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。

        String objectName = System.currentTimeMillis()+filename.substring(filename.lastIndexOf("."));
        objectName = "imgs/"+ objectName;


        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传文件。
            ossClient.putObject(putObjectRequest);

            // 得到上传之后的文件路径
            // https://bucketname.endpoint/objectName
            String fileUrl = "https://\t\n" +
                    "minissm2207.oss-cn-hangzhou.aliyuncs.com/"+objectName;

            System.out.println("上传之后的文件路径："+ fileUrl);
            return fileUrl;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return "上传失败";
    }




    public static void main(String[] args) throws FileNotFoundException {
       // FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Lenovo\\Pictures\\壁纸\\favorite1.png");
      //  uploadFile(fileInputStream,"favorite1.png");


        String str = "1,2,3,4,5";
        String[] split = str.split(",");

   /*     for (int i = 0; i <split.length; i++) {
            System.out.println(split[i]);

        }*/
        int[] ids = new int[split.length];

        for (int i = 0; i <split.length; i++) {
            ids[i]=Integer.parseInt(split[i]);
        }

        for (int i = 0; i < ids.length; i++) {
            System.out.println(ids[i]);
        }

    }


}
